package com.aaa.ssm.controller;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.UUID;

@RestController
@RequestMapping("oss")
public class OSSController {

    private static Properties properties =new Properties();

    static {
        //任意一个类的Class对象中都存在一个方法getResourceAsStream   可以把properties读成输入字节流
        InputStream inputStream = OSSController.class.getResourceAsStream("/oss.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("uploadFile")
    public static String uploadFile(@RequestParam MultipartFile multipartFile) {

        System.out.println(multipartFile);

        String filename = multipartFile.getOriginalFilename();
        System.out.println(filename+".............123..................");
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName ="scenic/pic/"+ filename;
        String[] nameArray = filename.split("\\.");
        objectName  = "scenic/pic/" + nameArray[0] +"-"+ UUID.randomUUID()+ "."+nameArray[1];

        String endpoint = properties.getProperty("oss.endpoint");
        String accessKeyId = properties.getProperty("oss.accessKeyId");
        String accessKeySecret = properties.getProperty("oss.accessKeySecret");
        String bucketName = properties.getProperty("oss.bucketName");
        String ossUrl = properties.getProperty("oss.url");

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            InputStream inputStream = multipartFile.getInputStream();
            ossClient.putObject(bucketName, objectName,inputStream);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        //https://qy168.oss-cn-hangzhou.aliyuncs.com/mihudan-d3d4436a-a67b-43f0-9aea-9526adce79b4.png
        return ossUrl + "/" + objectName;

    }
}

